package com.logistics.modules.warehouseManagement.service.impl;

import cn.hutool.core.convert.Convert;
import com.logistics.modules.warehouseManagement.dao.SeatDao;
import com.logistics.modules.warehouseManagement.entity.SeatEntity;
import com.logistics.modules.warehouseManagement.service.SeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.logistics.common.utils.PageUtils;
import com.logistics.common.utils.Query;

import com.logistics.modules.warehouseManagement.dao.RepositoryDao;
import com.logistics.modules.warehouseManagement.entity.RepositoryEntity;
import com.logistics.modules.warehouseManagement.service.RepositoryService;


@Service("repositoryService")
public class RepositoryServiceImpl extends ServiceImpl<RepositoryDao, RepositoryEntity> implements RepositoryService {

    @Autowired
    private SeatService seatService;

    @Autowired
    private SeatDao seatDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<RepositoryEntity> page = new Query<RepositoryEntity>().getPage(params);

        // 获取参数
        String repositoryName = Convert.toStr(params.get("repositoryName"));
        String repositoryAddress = Convert.toStr(params.get("repositoryAddress"));

        // 执行查询
        this.baseMapper.selectAllByRepositoryName(page, repositoryName, repositoryAddress);

        // 对page进行加工
        page = updatePage(page);

        return new PageUtils(page);
    }

    /**
     * 为所有的仓库实体加上百分比的数据
     *
     * @param page 页面
     */
    private IPage<RepositoryEntity> updatePage(IPage<RepositoryEntity> page) {
        // 得到里面的数据
        List<RepositoryEntity> records = page.getRecords();
        // 对仓库集合进行遍历
        for (RepositoryEntity ro : records) {
            // 拿到仓库id
            Integer id = ro.getId();
            // 根据仓库id得到 席位集合
            List<SeatEntity> seatEntities = seatDao.selectAllByRepositoryId(id);
            // 通过席位集合计算出仓库占用的百分比
            Integer percentage = new InitRepositoryServiceImpl().countSeatsUsedInASeatSet(seatEntities, ro);
            // 设置占用百分比
            ro.setStoreStatus(percentage);
        }

        return page;
    }

    /**
     * 插入
     *
     * @param repository 存储库
     */
    @Override
    public void insertRepositoryEntity(RepositoryEntity repository) {
        // 设置默认状态
        repository.setStoreStatus(0);

        // 保存到数据库
        this.save(repository);
    }

    /**
     * 选择所有仓库实体类
     *
     * @return {@link List}<{@link RepositoryEntity}>
     */
    @Override
    public List<RepositoryEntity> selectAll() {
        return this.baseMapper.selectList(null);
    }

    /**
     * 删除存储库由ids
     *
     * @param asList 正如列表
     */
    @Override
    public void removeRepositoryByIds(List<Integer> asList) {
        // 检车仓库中是否包含了货物
        if (checkRepositoryIncludeCargo(asList)) {
            throw new RuntimeException("您要删除的仓库中包含了客户保存的货物无法删除!");
        }
        // 删除全部仓库
        this.removeByIds(asList);
        // 根据仓库id去数据库删除席位
        this.seatService.removeByRepositoryIds(asList);
    }

    /**
     * 检查存储库中的席位是否包括货物
     *
     * @param asList 正如列表
     * @return boolean
     */
    private boolean checkRepositoryIncludeCargo(List<Integer> asList) {

        for (Integer id : asList) {
            // 通过仓库id去数据库
            int count = seatDao.countByRepositoryIdAndCargoId(id);
            if (count > 0) {
                return true;
            }
        }

        return false;
    }

    @Override
    public List<RepositoryEntity> selectAllByStartAddress(String startAddress) {
        return this.baseMapper.selectAllByStoreAddress(startAddress);
    }

}